package com.hackordie.operations.queries;

import com.hackordie.commons.HackOrDieResult;
import com.hackordie.commons.authorities.Role;
import com.hackordie.exceptions.HackOrDieException;
import com.hackordie.operations.AHackOrDieOperationRole;

public class QueryRole extends AHackOrDieOperationRole {

	private final int idRole;
	private final boolean allRoles;
	private final int first;
	private final int last;
	private final String nameRole;

	public QueryRole() {
		super();
		this.idRole = -1;
		this.allRoles = true;
		this.first = -1;
		this.last = -1;
		this.nameRole = "";
	}

	public QueryRole(int idRole) {
		super();
		this.idRole = idRole;
		this.allRoles = false;
		this.first = -1;
        this.last = -1;
        this.nameRole = "";
	}

	public QueryRole(int first, int last) {
	    this.idRole = -1;
        this.allRoles = false;
        this.first = first;
        this.last = last;
        this.nameRole = "";
    }
	
	public QueryRole(String nameRole) {
        this.idRole = -1;
        this.allRoles = false;
        this.first = -1;
        this.last = -1;
        this.nameRole = nameRole;
    }

    @Override
	protected boolean checkParameters() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	protected HackOrDieResult executeOperation() throws HackOrDieException {
		if (allRoles) {
			final Role[] roles = roleDao.getAll();
			result.setObject(roles);
		} else {
		    if(first != -1) {
		        final Role[] roles = roleDao.getByRange(first, last);
		        result.setObject(roles);		        
		    } else {
		        if(nameRole.equals("")) {
		            final Role role = roleDao.get(idRole);
		            result.setObject(role);
		        } else {
		            final Role role = roleDao.get(nameRole);
		            result.setObject(role);
		        }
		    }
		}
		return result;
	}
}
